<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            position: relative;
            top: 200px;
            left: 550px;
            width: 500px;
            height: 500px;
        }
        div span {
            display: inline-block;
            width: 100px;
            height: 100px;
            background-color: aqua;
            text-align: center;
            font-size: 20px;
            line-height: 100px;
        }
    </style>
</head>
<body>
    <div>
        <span class="hour">1</span>
        <span class="minute">2</span>
        <span class="second">3</span>   
    </div>
    <script>
        // 1. 获取元素
        var hour = document.querySelector('.hour');
        var minute = document.querySelector('.minute');
        var second = document.querySelector('.second');
        var inputTime = +new Date('2022-7-31 18:00:00'); // 预设的时间

        // 开启定时器
        setInterval(countDown, 1000);
        // 获取时间
        function countDown(time) {
            var nowTime = +new Date(); // 当前时间
            var times = (inputTime - nowTime) / 1000; //将剩余时间转换成秒
            var h = parseInt(times / 60 / 60 % 24); //时
            h = h < 10 ? '0' + h : h;
            hour.innerHTML = h;
            var m = parseInt(times / 60 % 60 ); //分钟
            m = m < 10 ? '0' + m : m;
            minute.innerHTML = m;
            var s = parseInt(times % 60 ); //秒
            s = s < 10 ? '0' + s : s;
            second.innerHTML = s;
            console.log('h:' + h + 'm:' + m + ' s:' + s);
        }
    </script>
</body>
</html>